﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace MS.Utility.Config
{
    #region 文件访问方式枚举
    /// <summary>
    /// 文件访问方式
    /// </summary>
    public enum FileAccessMode
    {
        /// <summary>
        /// 本地目录
        /// </summary>
        FileByLocal = 0,

        /// <summary>
        /// 文件服务器
        /// </summary>
        FileByUNC = 1
    }
    #endregion

    public class ConfigSetting
    {
        public static string TMP = "Tmp";//临时目录名称
      

        public const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
        public const string ShortDateFormat = "yyyy-MM-dd";
        public const string ShotrTimeFormat = "HH:mm:ss";
        public static readonly int TOP = 5;

        #region SevenZFile
        private static string _SevenZFile;

        public static string SevenZFile
        {
            get
            {
                if (_SevenZFile == null)
                {
                    _SevenZFile = ConfigurationManager.AppSettings["SevenZFile"].ToString();
                }
                return _SevenZFile;
            }
        }
        #endregion

        #region 用户登录错误最大次数
        /// <summary>
        /// 用户登录错误最大次数
        /// </summary>
        private static string _LoginWrongCount;

        public static string LoginWrongCount
        {
            get
            {
                if (_LoginWrongCount == null)
                {
                    _LoginWrongCount = ConfigurationManager.AppSettings["LoginWrongCount"].ToString();
                }
                return _LoginWrongCount;
            }
        }
        #endregion

        #region 用户登录错误最大次数
        /// <summary>
        /// 用户登录错误最大次数
        /// </summary>
        private static string _AllLoginTime;

        public static string AllLoginTime
        {
            get
            {
                if (_AllLoginTime == null)
                {
                    _AllLoginTime = ConfigurationManager.AppSettings["AllLoginTime"].ToString();
                }
                return _AllLoginTime;
            }
        }
        #endregion

        #region 默认密码
        /// <summary>
        /// 默认密码
        /// </summary>
        private static string _DefaultPassword;

        public static string DefaultPassword
        {
            get
            {
                if (_DefaultPassword == null)
                {
                    _DefaultPassword = ConfigurationManager.AppSettings["DefaultPassword"].ToString();
                }
                return _DefaultPassword;
            }
        }
        #endregion

        #region 组织机构管理员初始账户前缀
        /// <summary>
        /// 组织机构管理员初始账户前缀
        /// </summary>
        private static string _OrgManagerUserName;

        public static string OrgManagerUserName
        {
            get
            {
                if (_OrgManagerUserName == null)
                {
                    _OrgManagerUserName = ConfigurationManager.AppSettings["OrgManagerUserName"].ToString();
                }
                return _OrgManagerUserName;
            }
        }
        #endregion

        #region 机构管理员默认邮箱后缀名
        /// <summary>
        /// 机构管理员默认邮箱后缀名
        /// </summary>
        private static string _OrgManagerMail;
        /// <summary>
        /// 机构管理员默认邮箱后缀名
        /// </summary>
        public static string OrgManagerMail
        {
            get
            {
                if (_OrgManagerMail == null)
                {
                    _OrgManagerMail = ConfigurationManager.AppSettings["OrgManagerMail"].ToString();
                }
                return _OrgManagerMail;
            }
        }
        #endregion

        #region 文件根目录

        private static string _FileRootUrl;
        /// <summary>
        /// 数据文件根URL(以/结尾)
        /// </summary>
        public static string FileRootUrl
        {
            get
            {
                if (_FileRootUrl == null)
                {

                    string fileRootUrl = ConfigurationManager.AppSettings["FileRootUrl"].ToString();
                    if (fileRootUrl == string.Empty)//默认为~/Upload/
                    {
                        fileRootUrl = "/Upload/";
                    }
                    if (!fileRootUrl.EndsWith("/"))
                    {
                        fileRootUrl += "/";
                    }

                    _FileRootUrl = fileRootUrl;
                }

                return _FileRootUrl;
            }
        }

        private static string _FileRootPath;
        /// <summary>
        /// 数据文件根路径(以\结尾)
        /// </summary>
        internal static string FileRootPath
        {
            get
            {
                if (_FileRootPath == null)
                {
                    string fileRootPath = ConfigurationManager.AppSettings["FileRootPath"].ToString();
                    if (fileRootPath == string.Empty)
                    {
                        fileRootPath = System.Web.Hosting.HostingEnvironment.MapPath(FileRootUrl);
                    }

                    if (!fileRootPath.EndsWith(@"\"))
                    {
                        fileRootPath += @"\";
                    }

                    _FileRootPath = fileRootPath;
                }

                return _FileRootPath;
            }
        }

        #endregion

        #region 文件访问类型

        /// <summary>
        /// 文件访问类型
        /// </summary>
        public static FileAccessMode FileAccessType
        {
            get
            {
                return (FileAccessMode)Enum.Parse(typeof(FileAccessMode), ConfigurationManager.AppSettings["FileAccessMode"].ToString());
            }
        }
        #endregion


        #region 导入异常生成EXCEL路径
        private static string _ImportErrorUrl;

        public static string ImportErrorUrl
        {
            get
            {
                if (_ImportErrorUrl == null)
                {
                    _ImportErrorUrl = FileRootUrl + ConfigurationManager.AppSettings["ImportErrorUrl"].ToString();
                }
                return _ImportErrorUrl;
            }
        }

        private static string _ImportErrorPath;

        public static string ImportErrorPath
        {
            get
            {
                if (_ImportErrorPath == null)
                {
                    string importErrorPath = ConfigurationManager.AppSettings["ImportErrorPath"].ToString();
                    if (importErrorPath == string.Empty)
                    {
                        _ImportErrorPath = System.Web.Hosting.HostingEnvironment.MapPath(ImportErrorUrl);
                    }
                    else
                    {
                        _ImportErrorPath = FileRootPath + importErrorPath;
                    }
                }

                return _ImportErrorPath;
            }
        }

        /// <summary>
        /// 导入异常生成EXCEL文件Path
        /// </summary>
        /// <returns>文件路径</returns>
        public static string GetImportErrorPath()
        {
            return string.Format(@"{0}\", ImportErrorPath);
        }

        /// <summary>
        /// 导入异常生成EXCEL文件Url
        /// </summary>
        /// <returns>文件路径</returns>
        public static string GetImportErrorUrl()
        {
            return string.Format(@"{0}/", ImportErrorUrl);
        }

        #endregion

        #region 用户头像存放路径
        private static string _UserLogoUrl;

        public static string UserLogoUrl
        {
            get
            {
                if (_UserLogoUrl == null)
                {
                    _UserLogoUrl = FileRootUrl + ConfigurationManager.AppSettings["PortraitUrl"].ToString();
                }
                return _UserLogoUrl;
            }
        }

        private static string _UserLogoPath;

        public static string UserLogoPath
        {
            get
            {
                if (_UserLogoPath == null)
                {
                    string userLogoPath = ConfigurationManager.AppSettings["PortraitPath"].ToString();
                    if (userLogoPath == string.Empty)
                    {
                        _UserLogoPath = System.Web.Hosting.HostingEnvironment.MapPath(UserLogoUrl);
                    }
                    else
                    {
                        _UserLogoPath = FileRootPath + userLogoPath;
                    }
                }

                return _UserLogoPath;
            }
        }

        /// <summary>
        /// 获取用户头像文件目录Path
        /// </summary>
        /// <returns>文件路径</returns>
        public static string GetUserLogoPath()
        {
            return string.Format(@"{0}\", UserLogoPath);
        }
        /// <summary>
        /// 获取头像文件临时目录
        /// </summary>
        /// <param name="planCode"></param>
        /// <returns></returns>
        public static string GetUserLogoPathTmpPath()
        {
            return string.Format(@"{0}\{1}\", UserLogoPath, TMP);
        }

        /// <summary>
        /// 获取用户头像文件目录Url
        /// </summary>
        /// <returns>文件路径</returns>
        public static string GetUserLogoUrl()
        {
            return string.Format(@"{0}/", UserLogoUrl);
        }
        #endregion

        #region 首页轮播图存放路径
        private static string _CarouselDiagramLogoUrl;

        public static string CarouselDiagramLogoUrl
        {
            get
            {
                if (_CarouselDiagramLogoUrl == null)
                {
                    _CarouselDiagramLogoUrl = FileRootUrl + ConfigurationManager.AppSettings["CarouselDiagramLogoUrl"].ToString();
                }
                return _CarouselDiagramLogoUrl;
            }
        }

        private static string _CarouselDiagramLogoPath;

        public static string CarouselDiagramLogoPath
        {
            get
            {
                if (_CarouselDiagramLogoPath == null)
                {
                    string carouselDiagramLogoPath = ConfigurationManager.AppSettings["CarouselDiagramLogoPath"].ToString();
                    if (carouselDiagramLogoPath == string.Empty)
                    {
                        _CarouselDiagramLogoPath = System.Web.Hosting.HostingEnvironment.MapPath(CarouselDiagramLogoUrl);
                    }
                    else
                    {
                        _CarouselDiagramLogoPath = FileRootPath + carouselDiagramLogoPath;
                    }
                }

                return _CarouselDiagramLogoPath;
            }
        }

        /// <summary>
        /// 获取首页轮播图片文件目录Path
        /// </summary>
        /// <returns>文件路径</returns>
        public static string GetCarouselDiagramLogoPath()
        {
            return string.Format(@"{0}\", CarouselDiagramLogoPath);
        }
        /// <summary>
        /// 获取首页轮播图片文件临时目录
        /// </summary>
        /// <param name="planCode"></param>
        /// <returns></returns>
        public static string GetCarouselDiagramLogoPathTmpPath()
        {
            return string.Format(@"{0}\{1}\", CarouselDiagramLogoPath, TMP);
        }

        /// <summary>
        /// 获取首页轮播图片文件目录Url
        /// </summary>
        /// <returns>文件路径</returns>
        public static string GetCarouselDiagramLogoUrl()
        {
            return string.Format(@"{0}/", CarouselDiagramLogoUrl);
        }
        #endregion

        #region 上传文件大小
        private static int _UploadFileSize;
        /// <summary>
        /// 上传文件大小
        /// </summary>
        public static int UploadFileSize
        {
            get
            {
                if (_UploadFileSize == 0)
                {
                    string uploadFileSize = ConfigurationManager.AppSettings["UploadFileSize"].ToString();
                    if (uploadFileSize == string.Empty)
                    {
                        _UploadFileSize = 8388608;
                    }
                    else
                    {
                        _UploadFileSize = int.Parse(uploadFileSize);
                    }
                }

                return _UploadFileSize;
            }

        }
        #endregion

        #region 返回用户头像
        /// <summary>
        /// 获取用户头像的URL地址
        /// </summary>
        /// <param name="userLogo"></param>
        /// <returns></returns>
        public static string GetUserLogoAbsoluteUrl(string userLogo)
        {
            if (!string.IsNullOrEmpty(userLogo))
            {
                return ImageCDN + GetUserLogoUrl() + userLogo;
            }
            else
            {
                return ImageCDN + "/Images/nophoto.jpg";
            }
        }
        #endregion

        #region Banner图上传路径
        private static string _BannerImageUrl;

        public static string BannerImageUrl
        {
            get
            {
                if (_BannerImageUrl == null)
                {
                    _BannerImageUrl = FileRootUrl + ConfigurationManager.AppSettings["BannerImageUrl"].ToString();
                }
                return _BannerImageUrl;
            }
        }

        private static string _BannerImagePath;

        public static string BannerImagePath
        {
            get
            {
                if (_BannerImagePath == null)
                {
                    string orgLogoPath = ConfigurationManager.AppSettings["BannerImagePath"].ToString();
                    if (orgLogoPath == string.Empty)
                    {
                        _BannerImagePath = System.Web.Hosting.HostingEnvironment.MapPath(BannerImageUrl);
                    }
                    else
                    {
                        _BannerImagePath = FileRootPath + orgLogoPath;
                    }
                }

                return _BannerImagePath;
            }
        }

        /// <summary>
        /// 获取BannerImage文件Path
        /// </summary>
        /// <param name="planCode"></param>
        /// <returns>文件路径</returns>
        public static string GetBannerImagePath()
        {
            return string.Format(@"{0}\", BannerImagePath);
        }
        /// <summary>
        /// 获取BannerImage文件临时目录
        /// </summary>
        /// <param name="planCode"></param>
        /// <returns></returns>
        public static string GetBannerImagePathTmpPath()
        {
            return string.Format(@"{0}\{1}\", BannerImagePath, TMP);
        }

        /// <summary>
        /// 获取BannerImage文件Url
        /// </summary>
        ///
        /// <returns>文件路径</returns>
        public static string GetBannerImageUrl()
        {
            return string.Format(@"{0}/", BannerImageUrl);
        }
        #endregion

        private static string _CookiePath;
        /// <summary>
        /// 从配置中获取CookiePath的值
        /// </summary>
        public static string CookiePath
        {
            get
            {
                if (_CookiePath == null)
                {
                    _CookiePath = ConfigurationManager.AppSettings["CookiePath"].ToString();
                }
                return _CookiePath;
            }
        }

        private static string _EncryptKey;
        /// <summary>
        /// 从配置中获取EncryptKey的值
        /// </summary>
        public static string EncryptKey
        {
            get
            {
                if (_EncryptKey == null)
                {
                    _EncryptKey = ConfigurationManager.AppSettings["EncryptKey"].ToString();
                }
                return _EncryptKey;
            }
        }


        private static string _ImageCDN;
        /// <summary>
        /// 图片的CDN路径
        /// </summary>
        public static string ImageCDN
        {
            get
            {
                if (_ImageCDN == null)
                {
                    _ImageCDN = ConfigurationManager.AppSettings["ImageCDN"].ToString();
                }
                return _ImageCDN;

            }
        }

        private static List<string> whiteList;
        /// <summary>
        /// 获取用户白名单
        /// </summary>
        public static List<string> WhiteList
        {
            get
            {
                if (whiteList == null)
                {
                    whiteList = ConfigurationManager.AppSettings["WhiteList"].ToString().Split(',').ToList();
                }
                return whiteList;
            }
        } 
    }
}
